package gui;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import storage.statistics.Statistic;
import storage.statistics.StatisticType;
import common.EarthSimSettings;

public class OutputPanel extends JPanel {
  private static final long serialVersionUID = -8130648670494751518L;
  
  private EarthSimSettings settings = EarthSimSettings.getInstance();
  private static final String TAB = "&nbsp;&nbsp;";
  private JEditorPane editor;
  
  public OutputPanel() {
    super();
    settings.setOutputPanel(this);
    setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
    setBorder(BorderFactory.createTitledBorder("Output"));
    
    Dimension dim = new Dimension(250,310);
    setPreferredSize(dim);
    setMaximumSize(dim);
    editor = new JEditorPane();
    editor.setEditable(false);
    editor.setContentType("text/html");
//    editor.setText(getOutput());
    
    JButton btn = new JButton("Show Cell Values");
    btn.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
       CellValueWindow cells = new CellValueWindow();
       cells.showGUI();
      }
    });
    
    add(editor);
    add(btn);
  }
  
  public void update() {
    editor.setText(getOutput());
  }
  
  private String getOutput() {
    StringBuffer output = new StringBuffer();
    output.append("<html><font style=\"font-family:tahoma; font-size:9px\">")
          .append(getStatisticOutput(StatisticType.MIN_TEMP)).append("<br><br>")
          .append(getStatisticOutput(StatisticType.MAX_TEMP)).append("<br><br>")
          .append(getStatisticOutput(StatisticType.MEAN_TEMP_REGION)).append("<br><br>")
          .append(getStatisticOutput(StatisticType.MEAN_TEMP_TIME))
          .append("</font></html>");
    return output.toString();
  }
  
  private String getStatisticOutput(StatisticType type) {
    Statistic stat = settings.getStatistic(type);
    return (stat == null ? "" : stat.getHtmlString());
  }

}
